【例子介绍】
delphi封装的回调函数DLL,为了自动触发事件
【相关图片】
【源码结构】
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type THDProcedure=procedure(s:string); stdcall; Ttest = function (RecvBack:THDProcedure):integer;StdCall; TForm1 = class(TForm) Button2: TButton; Button1: TButton; procedure FormCreate(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; DLLHandle:THandle; procedure HdProExample(sExam:string);stdcall; function test(device:pointer):integer;stdcall;external 'PDLL.dll'; //静态 implementation {$R *.dfm} procedure HdProExample(sExam:string);stdcall; begin ShowMessage(sExam); end; procedure TForm1.FormCreate(Sender: TObject); begin //DLLHandle := LoadLibrary(PChar('PDLL.dll')); end; procedure TForm1.Button2Click(Sender: TObject); var D: String; Func: Ttest; begin @Func := GetProcAddress(DLLHandle, 'test'); if Assigned(@Func) then begin Func(@HdProExample); end; end; procedure TForm1.Button1Click(Sender: TObject); begin test(@HdProExample); end; end.
评论